﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace AzukiMap.MapSource
{
    public class KaMapSource : TileSource
    {
        const string DEFAULT_NAME = "kaMap tile";
        const int DEFAULT_TILE_WIDTH = 256;
        const int DEFAULT_TILE_HEIGHT = 256;
        const int DEFAULT_SRID = 4326;
        const CoordinationUnits DEFAULT_UNIT = CoordinationUnits.DEGREES;
        const int DEFAULT_DPI = 72;

        string _mapName;
        List<string> _layers = new List<string>();
        List<string> _groups = new List<string>();

        public List<string> Groups
        {
            get { return _groups; }
            set { _groups = value; }
        }

        public List<string> Layers
        {
            get { return _layers; }
            set { _layers = value; }
        }

        public string MapName
        {
            get { return _mapName; }
            set { _mapName = value; }
        }

        public KaMapSource(string name, List<string> baseUris, int srid, CoordinationUnits coordUnit, 
            int tileWidth, int tileHeight, List<double> scales, int dpi)
            : base(name, baseUris, srid, coordUnit, tileWidth, tileHeight, scales, dpi)
        {
        }

        public KaMapSource(string name, List<string> baseUris, int srid, CoordinationUnits coordUnit, 
            int tileWidth, int tileHeight, List<double> scales, int dpi,
            List<string> layerNames, List<string> groupNames)
            : base(name, baseUris, srid, coordUnit, tileWidth, tileHeight, scales, dpi)
        {
            _layers = layerNames;
            _groups = groupNames;
        }

        public KaMapSource()
            : base(DEFAULT_NAME, DEFAULT_SRID, DEFAULT_UNIT)
        {
            _tileWidth = DEFAULT_TILE_WIDTH;
            _tileHeight = DEFAULT_TILE_HEIGHT;
            _dpi = DEFAULT_DPI;
        }

        public override XElement ToXml()
        {
            return ToXml("KaMapSource");
        }

        public override XElement ToXml(string elementName)
        {
            /*
             <xs:attribute name="MapName" type="xs:string" />
             <xs:choice>
                <xs:element name="Layers">
                  <xs:complexType>
                    <xs:choice>
                      <xs:element minOccurs="0" maxOccurs="unbounded" name="LayerName" type="xs:string" />
                    </xs:choice>
                  </xs:complexType>
                </xs:element>
                <xs:element name="Groups">
                  <xs:complexType>
                    <xs:choice>
                      <xs:element minOccurs="0" maxOccurs="unbounded" name="GroupName" type="xs:string" />
                    </xs:choice>
                  </xs:complexType>
                </xs:element>
                <xs:group ref="MapSourceElements" />
             </xs:choice>
            */
            XElement kaMapSourceElement = base.ToXml(elementName);
            kaMapSourceElement.Add(
                    _xmlCommon.CreateAttr("MapName", _mapName),
                    new XElement("Layers",
                        from layerName in _layers
                        select new XElement("LayerName", layerName)
                    ),
                    new XElement("Groups",
                        from groupName in _groups
                        select new XElement("GroupName", groupName)
                    )
            );

            return kaMapSourceElement;
        }

        public override void Parse(XElement mapSourceXml)
        {
            base.Parse(mapSourceXml);
            XElement tileSourceXml = mapSourceXml.Element("TileSource");
            XElement kamapSourceXml = tileSourceXml.Element("KamapSource");
            _mapName = _xmlCommon.GetStringElement(kamapSourceXml, "MapName");
            _layers = (List<string>)
                from layer in kamapSourceXml.Element("Layers").Elements("Layer") 
                select layer; 
            _groups = (List<string>)
                from groupName in kamapSourceXml.Element("Groups").Elements("GroupName") 
                select groupName;
        }

    }
}
